package com.seu.project.seuquiz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seu.project.seuquiz.common.ErrorCode;
import com.seu.project.seuquiz.constant.CommonConstant;
import com.seu.project.seuquiz.exception.BusinessException;
import com.seu.project.seuquiz.mapper.ContestMapper;
import com.seu.project.seuquiz.model.dto.contest.ContestQueryRequest;
import com.seu.project.seuquiz.model.entity.CollegeContestMap;
import com.seu.project.seuquiz.model.entity.Contest;
import com.seu.project.seuquiz.service.ContestService;
import com.seu.project.seuquiz.util.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author lenovo
* @description 针对表【contest(竞赛)】的数据库操作Service实现
* @createDate 2023-07-15 13:47:58
*/
@Service
public class ContestServiceImpl extends ServiceImpl<ContestMapper, Contest>
    implements ContestService {

    @Override
    public long contestAdd(String contestName, Date startTime, Date endTime, Integer answerTimes, Integer time, String questionNum, String questionList) {
        if (StringUtils.isAnyBlank(contestName, questionNum, questionList)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "竞赛名称、问题数、问题选项数不能为空");
        }
        if (startTime == null || endTime == null || startTime.after(endTime)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "起始、结束日期不能为空，且起始日期早于结束日期");
        }
        if (answerTimes == null || time == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "回答次数、时间不能为空");
        }
        if (answerTimes <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "回答次数不能小于等于0");
        }

        synchronized (contestName) {
            Contest contest = new Contest();
            contest.setContestName(contestName);
            contest.setStartTime(startTime);
            contest.setEndTime(endTime);
            contest.setAnswerTimes(answerTimes);
            contest.setTime(time);
            contest.setQuestionNum(questionNum);
            contest.setQuestionList(questionList);

            boolean save = this.save(contest);
            if(!save){
                throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加失败，数据库错误");
            }
            return contest.getId();
        }
    }

    @Override
    public QueryWrapper<Contest> getQueryWrapper(ContestQueryRequest request) {
        QueryWrapper<Contest> queryWrapper = new QueryWrapper<>();
        if (request == null) {
            return null;
        }

        Long id = request.getId();
        String contestName = request.getContestName();
//        Date startTime = request.getStartTime();
//        Date endTime = request.getEndTime();
        Integer answerTimes = request.getAnswerTimes();
        Integer time = request.getTime();
        String questionNum = request.getQuestionNum();
        String questionList = request.getQuestionList();
        Integer status = request.getStatus();
//        Date createTime = request.getCreateTime();
//        Date updateTime = request.getUpdateTime();
        String sortField = request.getSortField();
        String sortOrder = request.getSortOrder();

        queryWrapper.eq(id != null && id > 0,"id", id);
        queryWrapper.like(Strings.isNotBlank(contestName), "contestName", "%" + contestName + "%");
        queryWrapper.eq(answerTimes != null && answerTimes > 0,"answerTimes", answerTimes);
        queryWrapper.eq(time != null && time > 0,"time", time);
        queryWrapper.eq(Strings.isNotBlank(questionNum), "questionNum", questionNum);
        queryWrapper.eq(Strings.isNotBlank(questionList), "questionList", questionList);
        queryWrapper.eq(status != null && status > 0,"status", status);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);

        return queryWrapper;
    }
}




